/**
 * The number, 197, is called a circular prime because 
 * all rotations of the digits: 197, 971, and 719, are themselves prime. 
 * There are thirteen such primes below 100: 
 * 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97. 
 * How many circular primes are there below one million?
 */

/**
 * @author TrinhNX
 * @start_date	: 2015/04/24
 * @end_date 	:
 */
public class Euler035 {
//	private static final int[] CHECK = { 0, 1, 0, 1, 0, 0, 0, 1, 0, 1 };

	private static final int[] VALID = { 1, 3, 7, 9 };

	public static void main(String[] args) {

		final long start = System.currentTimeMillis();
		int counter = 0;
		int value;

		for (int temp1 : VALID) {
			for (int temp2 : VALID) {
				for (int temp3 : VALID) {
					value = temp1 * 100 + temp2 * 10 + temp3;
					if (checkPrimeArray(circular(value))) {
						counter++;
					}
					for (int temp4 : VALID) {
						value = temp1 * 1000 + temp2 * 100 + temp3 * 10 + temp4;
						if (checkPrimeArray(circular(value))) {
							counter++;
						}
						for (int temp5 : VALID) {
							value = temp1 * 10000 + temp2 * 1000 + temp3 * 100 + temp4 * 10 + temp5;
							if (checkPrimeArray(circular(value))) {
								counter++;
							}
							for (int temp6 : VALID) {
								value = temp1 * 100000 + temp2 * 10000 + temp3 * 1000 + temp4 * 100 + temp5 * 10
										+ temp6;
								if (checkPrimeArray(circular(value))) {
									counter++;
								}
							}

						}

					}

				}

			}

		}

//				int[] data;
//				boolean checkPrime;
//				for (int value = 111; value < MAX; value = value + 2) {
//					// If prime
//					// Check valid characters
//					// Get all number available from circular, check prime
//					if (Common.isPrime(value)) {
//						data = circular(value);
//						checkPrime = true;
//						if (data != null) {
//							for (int temp : data) {
//								if (!Common.isPrime(temp)) {
//									checkPrime = false;
//								}
//							}
//							if (checkPrime) {
//								counter++;
//							}
//						}
//					}
//				}
		System.out.println(counter + 13);
		System.out.println("End after: " + (System.currentTimeMillis() - start));
	}

	/**
	 * Generate the circular from number
	 * @author		: TrinhNX
	 * @param number
	 * @return
	 * @start_date	: 2015/04/24
	 * @end_date	:
	 * ex: 123 -> 3, 2, 1
	 */
	private static int[] circular(final int number) {
		int index = 0;
		final int length = Common.numberLength(number);
		int[] result = new int[length];
		int m = number, n = 10;
		int base10 = Common.power10(length);
		int exp10 = 1;
		while(n > 9 ) {
			n = number % base10;
			m = number / base10;
			result[index++] = n * exp10 + m;
			base10 /= 10;
			exp10 *= 10;
		} 
		return result;
	}

	private static boolean checkPrimeArray(final int[] data) {
		boolean checkPrime = true;
		if (data != null) {
			for (int temp : data) {
				if (!Common.isPrime(temp)) {
					checkPrime = false;
					break;
				}
			}
		} else return false;
		return checkPrime;
	}
}
